#include "gamewindow.h"
#include "ui_gamewindow.h"
#include"config.h"
#include<QWidget>
#include<QTimer>
#include<QPainter>
#include<QKeyEvent>
#include<ctime>

GameWindow::GameWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::GameWindow)
{
    ui->setupUi(this);
    initScene();
    playGame();
    mPlaneMoveTimer=new QTimer(this);
    mPlaneMoveTimer->start(20);
    connect(mPlaneMoveTimer,&QTimer::timeout,this,&GameWindow::PlaneMove);
}

GameWindow::~GameWindow()
{
    delete ui;
}

void GameWindow::initScene()
{
    setFixedSize(MAIN_WIDTH,MAIN_HEIGHT);
    setWindowTitle(GAME_TITLE);
    setWindowIcon(QIcon(":/picture/images/me1.png"));
    m_Timer.setInterval(GAME_RATE);
    m_recorder=0;
    //随机数种子
    srand((unsigned int)time(NULL));
}

void GameWindow::keyPressEvent(QKeyEvent *event)
{
    switch (event->key()) {
    case Qt::Key_W:
    case Qt::Key_S:
    case Qt::Key_A:
    case Qt::Key_D:
    mKeyList.append(event->key());
    break;
    }
}

void GameWindow::keyReleaseEvent(QKeyEvent *event)
{
    if(mKeyList.contains(event->key()))
    {
        mKeyList.removeOne(event->key());
    }
}

void GameWindow::PlaneMove()
{
    for(int keycode : mKeyList)
    {
        switch(keycode)
        {
        case Qt::Key_W: m_plane.setPosition(m_plane.m_x,m_plane.m_y-5);break;
        case Qt::Key_S: m_plane.setPosition(m_plane.m_x,m_plane.m_y+5);break;
        case Qt::Key_A: m_plane.setPosition(m_plane.m_x-5,m_plane.m_y);break;
        case Qt::Key_D: m_plane.setPosition(m_plane.m_x+5,m_plane.m_y);break;
        }
    }
    if(m_plane.m_x<0){m_plane.m_x=0;}
    if(m_plane.m_y<0){m_plane.m_y=0;}
    if(m_plane.m_x>GAME_WIDTH-m_plane.m_Rect.width()){m_plane.m_x=GAME_WIDTH-m_plane.m_Rect.width();}
    if(m_plane.m_y>GAME_HEIGHT-m_plane.m_Rect.height()){m_plane.m_y=GAME_HEIGHT-m_plane.m_Rect.height();}
}

void GameWindow::playGame()
{
    m_Timer.start();
    connect(&m_Timer,&QTimer::timeout,[=](){
        //敌机出场
        enemyToScene();
        //大boss出场
        bossToScene();
        //地图滚动
        updatePosition();
        update();
        collisionDetecion();
    });
}

void GameWindow::updatePosition()
{
    m_map.mapPosition();
    m_plane.shoot();
    for(int i=0;i<BULLET_NUM;i++)
    {
        if(m_plane.m_bullet[i].m_free==false)
        {
            m_plane.m_bullet[i].updatePosition();
        }
    }
    //敌机出场
    for(int i=0;i<ENEMY_NUM;i++)
    {
        if(m_enemy[i].m_free==false)
        {
            m_enemy[i].updatePosition();
        }
    }
    //大boss出场
    if(m_boss.m_free==false)
        m_boss.updatePosition();
    //爆炸图片播放
    for(int i=0;i<BOMB_NUM;i++)
    {
        if(m_bomb[i].m_free==false)
        {
            m_bomb[i].updateInfo();
        }
    }
    for(int i=0;i<BOMB_NUM;i++)
    {
        if(m_bomb2[i].m_free==false)
        {
            m_bomb2[i].updateInfo();
        }
    }
}

void GameWindow::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.drawPixmap(0,m_map.m_map1_posY,m_map.m_map1);
    painter.drawPixmap(0,m_map.m_map2_posY,m_map.m_map2);
    if(m_plane.m_free)
    {
        painter.drawPixmap(m_plane.m_x,m_plane.m_y,m_plane.m_plane);
    }
    for(int i=0;i<BULLET_NUM;i++)
    {
        if(m_plane.m_bullet[i].m_free==false)
        {
            painter.drawPixmap(m_plane.m_bullet[i].m_x,m_plane.m_bullet[i].m_y,m_plane.m_bullet[i].m_bullet);
        }
    }
    for(int i=0;i<ENEMY_NUM;i++)
    {
        if(m_enemy[i].m_free==false)
        {
            painter.drawPixmap(m_enemy[i].m_x,m_enemy[i].m_y,m_enemy[i].m_enemy);
        }
    }
    if(m_boss.m_free==false)
        painter.drawPixmap(m_boss.m_x,m_boss.m_y,m_boss.m_boss);
    for (int i=0;i<BOMB_NUM ;i++ )
    {
        painter.drawPixmap(m_bomb[i].m_x,m_bomb[i].m_y,m_bomb[i].m_pixArr[m_bomb[i].m_index]);
    }
    for (int i=0;i<BOMB_NUM ;i++ )
    {
        painter.drawPixmap(m_bomb2[i].m_x,m_bomb2[i].m_y,m_bomb2[i].m_pixArr2[m_bomb2[i].m_index]);
    }
}

void GameWindow::enemyToScene()
{
    m_recorder++;
    if(m_recorder<ENEMY_INTERVAL){return;}
    m_recorder=0;
    for(int i=0;i<ENEMY_NUM;i++)
    {
        if(m_enemy[i].m_free)
        {
            m_enemy[i].m_free=false;
            m_enemy[i].m_x=rand()%(GAME_WIDTH-m_enemy[i].m_Rect.width());
            m_enemy[i].m_y=-m_enemy[i].m_Rect.height();
            break;
        }
    }
}

void GameWindow::bossToScene()
{
    m_recorder2++;
    if(m_recorder2<BOSS_INTERVAL){return;}
    m_recorder2=0;
    if(m_boss.m_free)
    {
        m_boss.m_free=false;
        m_boss.m_x=rand()%(GAME_WIDTH-m_boss.m_Rect.width());
        m_boss.m_y=-m_boss.m_Rect.height();
    }
}

void GameWindow::collisionDetecion()
{
    for(int i=0;i<ENEMY_NUM;i++)
    {
        if(m_enemy[i].m_free)
        {
            continue;
        }
        for (int j=0;j<BULLET_NUM ;j++ )
        {
            if(m_plane.m_bullet[j].m_free)
            {
                continue;
            }
            if(m_enemy[i].m_Rect.intersects(m_plane.m_bullet[j].m_Rect)||m_enemy[i].m_Rect.intersects(m_plane.m_Rect))
            {
                m_enemy[i].m_free=true;
                m_plane.m_bullet[j].m_free=true;
                for(int k=0;k<BOMB_NUM;k++)
                {
                    if(m_bomb[k].m_free)
                    {
                        m_bomb[k].m_free=false;
                        m_bomb[k].m_x=m_enemy[i].m_x;
                        m_bomb[k].m_y=m_enemy[i].m_y;
                        break;
                    }
                }
            }
        }
    }
    if(m_boss.m_Rect.intersects(m_plane.m_Rect))
    {
        m_plane.m_free=false;
        for(int i=0;i<BOMB_NUM;i++)
        {
            m_bomb2[i].m_free=false;
            m_bomb2[i].m_x=m_plane.m_x;
            m_bomb2[i].m_y=m_plane.m_y;
        }
    }
}


void GameWindow::on_pushButton_clicked()
{
    close();
}
